//
//  FFJPushManager.m
//  Common
//
//  Created by IT.Bear on 16/7/6.
//  Copyright © 2016年 IT.Bear. All rights reserved.
//

#import "FFJPushManager.h"
#import "JPUSHService.h"
#import <UIKit/UIKit.h>
#import <AdSupport/ASIdentifierManager.h>
#import <UserNotifications/UserNotifications.h>
#import "FFReceiverMessageHandler.h"

#define JPUSHKEY @"4d865a410ceb548eed8317d7"


static FFJPushManager *jpushManger;

@implementation FFJPushManager

+ (FFJPushManager *)sharedInstance {
    static dispatch_once_t oncetoken;
    dispatch_once(&oncetoken,^{
        if (!jpushManger) {
            jpushManger = [[self alloc] init];
        }
    });
    return jpushManger;
}

#pragma mark 极光推送
- (void)jPushRegisterWithOptions:(NSDictionary *)launchOptions{

    //Required
    // init Push(2.1.5版本的SDK新增的注册方法，改成可上报IDFA，如果没有使用IDFA直接传nil  )
    // 如需继续使用pushConfig.plist文件声明appKey等配置内容，请依旧使用[JPUSHService setupWithOption:launchOptions]方式初始化。
//    BOOL aps = NO;
//    if (CURRENT_SERVER_TYPE > 2) {
//        aps = YES;
//    }
    [JPUSHService setupWithOption:launchOptions appKey:JPUSHKEY
                          channel:nil
                 apsForProduction:NO
            advertisingIdentifier:nil];
}

#pragma mark - 向服务器上报Device Token
- (void)registerDeviceToken:(NSData *)deviceToken{
    [JPUSHService registerDeviceToken:deviceToken];
}

#pragma mark - 处理收到的APNS消息，向服务器上报收到APNS消息并且APP对推送消息进行处理
- (void)handleRemoteNotification:(NSDictionary *)userInfo{
    [JPUSHService handleRemoteNotification:userInfo];
    [self receivePush:userInfo];
}


#pragma mark - 收到推送处理
- (void)receivePush:(NSDictionary *)userInfo{
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    //聊天信息
    if ([userInfo[@"type"] isEqual:@"jmessage"]) {
        NSString *jmessageName = userInfo[@"content"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"jumpeJmessage" object:nil userInfo:@{@"jmessageName":jmessageName}];

    }
    
}

- (void)addTags:(NSString *)tag
{
    [JPUSHService addTags:[NSSet setWithObjects:tag, nil] completion:nil seq:NSNotFound];
}

@end
